<?php
/**
 * CreateTime: 2022/5/30 15:35
 */

namespace App\Http\Controllers\Lending;

use App\Consts\ClientConsts\ClientConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\ResponseFunc;
use App\Func\TokenFuc;
use App\Http\Controllers\Controller;
use App\Http\Requests\Lending\AddLendingPost;
use App\Http\Requests\ListBasicRequest;
use App\Service\Lending\LendingService;
use Lcobucci\JWT\Token;

class LendingController extends Controller
{
    private LendingService $lendingService;

    public function __construct()
    {
        $this->lendingService	= new LendingService()			;
        $this->m_cHelper		= app()->gcHelper				;

    }

    public function addLending(AddLendingPost $addLendingPost){
        $data = $addLendingPost->all();
        return ResponseFunc::returnJson(
            'ok',
            $this->lendingService->addLending($data),
            '新增借阅成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function getSelfStaffLending(ListBasicRequest $request){
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->lendingService->getSelfStaffLending($pageSize,$filters),
            '获取借阅记录成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }

    public function lendingRecord(ListBasicRequest $request){
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->lendingService->lendingRecord($pageSize,$filters),
            '获取借阅记录成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }



	/* 获取所有归档的合同
	 *
	 */
    public function getLendingList(ListBasicRequest $request){
        $pageSize = $request->get('page_size');
        $filters = $request->get('filters');
        return ResponseFunc::returnJson(
            'ok',
            $this->lendingService->getLendingListEx($pageSize,$filters),
            '获取档案库详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
//        return $this->lendingService->getLendingListEx($pageSize,$filters);

        //		return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (
//			$this->m_cHelper->gfGetRequestBodyValue(),
//			function($j)  {
//				$j->setDecodeStatus(
//                    $this->lendingService->getLendingListEx($j),
//                    'getLendingListEx.1000',
//                    "获取所有归档的合同".'成功'
//                );
//			},
//			function($j) {
//				$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'getLendingListEx.2000','获取所有归档的合同'.'失败');
//			}
//		)->getDecodeStatus());

		//        $pFilters	= $request->get('filters');
		//        $pageSize	= $request->get('page_size');
		//        $page		= $request->get('page');
		//        $filters = $pFilters ? json_decode($pFilters,true) : [];
		//        # $filters['client_status'] = ClientConsts::CLIENT_BLACK_LIST;
		//
		//        return ResponseFunc::returnJson(
		//            'ok',
		//            $this->lendingService->getLendingList($pageSize,$page,$filters),
		//            '获取合同档案的借阅资料库列表',
		//            ResponseStatusConsts::OK_200_RESPONSE_STATUS
		//        );
    }

    public function getLendingApplyInfo(int $lendingId){
        return ResponseFunc::returnJson(
            'ok',
            $this->lendingService->getLendingApplyInfo($lendingId),
            '获取借阅详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }


    public function getLendingInfo(int $lendingId){
        return ResponseFunc::returnJson(
            'ok',
            $this->lendingService->getLendingInfo($lendingId),
            '获取借阅详情成功',
            ResponseStatusConsts::OK_200_RESPONSE_STATUS
        );
    }


	/* 获取我的借阅列表
	 *
	 */
	public function archivesBorrowList(){
        $staff_id = TokenFuc::getCurrentStaffInfo()['staff_id'];
		return $this->m_cHelper->gfApiEchoEx($this->m_cHelper->gfJsonDecode (

			$this->m_cHelper->gfGetRequestBodyValue(),
			function($j) use($staff_id) {
				
				$j->appendProperty(['staff_id' => $staff_id]);
				$j->setDecodeStatus( $this->lendingService->archivesBorrowList($j),'archivesBorrowList.1000',"获取我的借阅列表".'成功');
			},
			function($j) {

				$j->setDecodeStatus(array(array('json.error : ' => $j->getOriginJsonString())),'archivesBorrowList.2000','获取我的借阅列表'.'失败');
			}
		)->getDecodeStatus());
	}
}